<template>
  <div class="container">
    <el-button
      type="primary"
      class="add"
      size="small"
      icon="el-icon-plus"
      style="margin-bottom: 15px"
      @click="handelAddDomain"
    >
      添加
    </el-button>
    <el-table :data="dataSource">
      <el-table-column label="域名" prop="domain" />
      <el-table-column label="注册时间" prop="register_date" />
      <el-table-column label="过期时间" prop="expire_date" />
      <el-table-column label="创建时间" prop="create_time" />
      <el-table-column label="提示" prop="hint" />
      <el-table-column label="操作" width="280px" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            class="edit"
            @click="handelAddDomain(scope.row)"
          >
            编辑
          </el-button>
          <el-button size="mini" class="blue" @click="handleUpdate(scope.row)">
            更新Whois
          </el-button>
          <el-button size="mini" class="dele" @click="handleDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page.currentPage"
      :page-sizes="page.pageSizes"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      :hide-on-single-page="page.total < page.size ? true : false"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <EditDomain
      :domain-info="domainInfo"
      :dialog-show.sync="dialogShow"
      @success="getListData"
    />
  </div>
</template>

<script>
import domainApi from '@/api/domain'
import EditDomain from './EditDomain.vue'
export default {
  components: {
    EditDomain
  },
  data() {
    return {
      dataSource: [],
      dialogShow: false,
      addShow: false,
      domainInfo: {},
      val: {},
      page: {
        total: 0,
        currentPage: 1,
        size: 10,
        pageSizes: [10, 20, 30, 40]
      } // 分页器
    }
  },
  mounted() {
    this.getListData()
  },
  methods: {
    async handleUpdate(row) {
      const res = await domainApi.updateDomain({ id: row.id })
      if (res.code == 0) {
        this.$message.success(res.msg)
      }
    },
    handelAddDomain(row = {}) {
      this.domainInfo = row
      this.dialogShow = true
    },
    async getListData() {
      this.val.page = this.page.currentPage
      this.val.limit = this.page.size

      const res = await domainApi.domainList(this.val)
      this.dataSource = res.data
      this.page.total = res.count
    },
    async handleDelete(row) {
      this.$confirm('您确定要删除该表单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await domainApi.deleteDomain({ id: row.id })
          if (res.code == 0) {
            this.getListData()
            this.$message.success(res.msg)
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(() => {})
    },
    // 分页规格改变
    handleSizeChange(val) {
      this.page.size = val
      this.page.currentPage = 1
      this.getListData()
    },
    // 分页点击
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getListData()
    }
  }
}
</script>

<style lang="scss" scope></style>
